{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1ce1fbe6-b2d5-4d2f-94ec-a3f031b49f19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "13\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "# find() 查找子串在字符串里面第一次出现的位置，没有则返回-1，find可以指定开始结束区间内查找\n",
    "s1 = \"Hello Python Hello\"\n",
    "print(s1.find(\"y\")) # 7\n",
    "\n",
    "#指定开始区间\n",
    "print(s1.find(\"H\", 12)) # 13\n",
    "\n",
    "#指定开始结束区间\n",
    "print(s1.find(\"o\", 12, 15)) # -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ad4739c2-dd89-4967-8800-58f014acf51a",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "substring not found",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[18], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# index() 区别和find差不多，唯一不一样的是没有找到会报错，而find不会\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(s1\u001b[38;5;241m.\u001b[39mindex(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n",
      "\u001b[1;31mValueError\u001b[0m: substring not found"
     ]
    }
   ],
   "source": [
    "# index() 区别和find差不多，唯一不一样的是没有找到会报错，而find不会\n",
    "print(s1.index(\"A\")) # ValueError: substring not found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "10f1b81e-0b4c-41c6-977a-e59615921505",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# count() 统计子串在字符串中出现的次数\n",
    "print(s1.count(\"Hello\")) # 2 次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fc1966a0-0585-4c2a-ba1a-ef8ab7409448",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*ello Python *ello\n"
     ]
    }
   ],
   "source": [
    "# replace() 字符串替换\n",
    "s2 = s1\n",
    "s2 = s2.replace(\"H\", \"*\")\n",
    "print(s2) # *ello Python *ello"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "bba9b936-ec47-4639-ba08-560de921ca10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello', 'Python Hello']\n",
      "Hello/Python Hello\n"
     ]
    }
   ],
   "source": [
    "# split(按什么拆分, 拆多少个) 字符串分割   按什么拼接.join(拼接谁) 字符串拼接\n",
    "s3 = s1.split(\" \",1) \n",
    "print(s3) # ['Hello', 'Python', 'Hello']\n",
    "s4 = \"/\".join(s3)\n",
    "print(s4) # Hello/Python/Hello"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e4918e58-a9bc-4fea-90b4-29fe257b60ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello python hello\n",
      "HELLO PYTHON HELLO\n",
      "Hello python hello\n",
      "Hello Python Hello\n",
      "hELLO pYTHON hELLO\n"
     ]
    }
   ],
   "source": [
    "# capitalize()、lower()、upper()、title()、swapcase()\n",
    "\"\"\" \n",
    "lower 方法和 upper 方法分别用于将字符串的所有字母字符转换为小写和大写。\n",
    "capitalize方法将字符串的第一个字符转换成大写。\n",
    "title 方法将字符串的每个单词首字母大写。\n",
    "swapcase 方法将字母大小写转换。\n",
    "\"\"\"\n",
    "print(s1.lower()) # hello python hello\n",
    "print(s1.upper()) # HELLO PYTHON HELLO\n",
    "print(s1.capitalize()) # Hello python hello\n",
    "print(s1.title()) # Hello Python Hello\n",
    "print(s1.swapcase()) # hELLO pYTHON hELLO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "8a3e45a4-7be6-4ab9-8394-3b28b547805e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否以字符 h 开头: True\n",
      "是否以字符 h 结尾: False\n"
     ]
    }
   ],
   "source": [
    "# startswith()、endswith() 字符串以什么开始和结束\n",
    "print('是否以字符 h 开头:',s1.startswith('H')) # 是否以字符 h 开头: True\n",
    "print('是否以字符 h 结尾:',s1.endswith('h')) #是否以字符 h 结尾: False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3599705-a18c-4a90-a951-bc8a32ae6327",
   "metadata": {},
   "outputs": [],
   "source": [
    "# center()、ljust()、rjust() 这几个方法同前面的format()方法相同，只不过更简单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66048936-9aa8-45a5-9b0d-f0b3f1f2fa93",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\" \n",
    "isalnum() 是否为字母或数字\n",
    "isalpha() 检测字符串是否只由字母组成(含汉字)\n",
    "isdigit() 检测字符串是否只由数字组成\n",
    "isspace() 检测是否为空白符\n",
    "isupper() 是否为大写字母\n",
    "islower() 是否为小写字母\n",
    "\"\"\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
